package net.j4love.grpc.demo.service;

import com.google.protobuf.ByteString;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.stub.StreamObserver;
import org.junit.Test;

import java.util.Iterator;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import static net.j4love.grpc.demo.LocalEnvConfiguration.CODE_ABSOLUTE_PATH;
import static net.j4love.grpc.demo.LocalEnvConfiguration.CODE_PATH;
import static net.j4love.grpc.demo.LocalEnvConfiguration.PROTOBUF_COMPILER_ABSOLUTE_PATH;
import static net.j4love.grpc.demo.LocalEnvConfiguration.PROTO_PATH;
import static net.j4love.grpc.demo.ProtobufCompilerUtils.generateJavaCode;

/**
 * @author He Peng
 * @create 2018-05-13 14:45
 * @update 2018-05-13 14:45
 * @updatedesc : 更新说明
 * @see
 */
public class GrpcServiceTest {

    @Test
    public void generateServiceCode() throws Exception {
        String protobufFilePath = CODE_ABSOLUTE_PATH + "/src/main/java/net/j4love/grpc/demo/proto/greeter_service.proto";
        generateJavaCode(PROTOBUF_COMPILER_ABSOLUTE_PATH , PROTO_PATH , CODE_PATH , protobufFilePath);

    }

    @Test
    public void unaryRPCTest() throws Exception {
        ManagedChannel channel = ManagedChannelBuilder
                .forAddress("192.168.1.137", 6666)
                .usePlaintext().build();
        // 同步调用
        GreeterServiceGrpc.GreeterServiceBlockingStub serviceBlockingStub =
                GreeterServiceGrpc.newBlockingStub(channel);
        GreeterServiceWrapper.HelloRequest helloRequest =
                GreeterServiceWrapper.HelloRequest
                        .newBuilder()
                        .setNameBytes(ByteString.copyFromUtf8("小猪佩琪"))
                        .build();
        System.out.println("Serialized Size = " + helloRequest.getSerializedSize());
        GreeterServiceWrapper.HelloReply helloReply = serviceBlockingStub.sayHello(helloRequest);
        System.out.println("gRPC call remote service response : " + helloReply.getMessage());
        channel.shutdown().awaitTermination(300 , TimeUnit.MILLISECONDS);
    }

    @Test
    public void serverStreamingRPCTest() throws Exception {
        ManagedChannel channel = ManagedChannelBuilder
                .forAddress("localhost", 6666)
                .usePlaintext()
                .build();
        GreeterServiceGrpc.GreeterServiceBlockingStub serviceBlockingStub =
                GreeterServiceGrpc.newBlockingStub(channel);
        GreeterServiceWrapper.HelloRequest helloRequest =
                GreeterServiceWrapper.HelloRequest
                        .newBuilder()
                        .setNameBytes(ByteString.copyFromUtf8("小猪佩琪"))
                        .build();
        Iterator<GreeterServiceWrapper.HelloReply> replies = serviceBlockingStub.lotsOfReplies(helloRequest);
        if (Objects.nonNull(replies)) {
            while (replies.hasNext()) {
                System.out.println("gRPC call remote service response : " + replies.next().getMessage());
            }
        }
        channel.shutdown().awaitTermination(300 , TimeUnit.MILLISECONDS);
    }

    @Test
    public void clientStreamingRPCTest() throws Exception {
        ManagedChannel channel = ManagedChannelBuilder
                .forAddress("localhost", 6666)
                .usePlaintext()
                .build();
        GreeterServiceGrpc.GreeterServiceStub serviceStub = GreeterServiceGrpc.newStub(channel);
        GreeterServiceWrapper.HelloRequest helloRequest =
                GreeterServiceWrapper.HelloRequest
                        .newBuilder()
                        .setNameBytes(ByteString.copyFromUtf8("小猪佩琪"))
                        .build();
        StreamObserver<GreeterServiceWrapper.HelloRequest> r = serviceStub.lotsOfGreetings(new StreamObserver<GreeterServiceWrapper.HelloReply>() {
            @Override
            public void onNext(GreeterServiceWrapper.HelloReply helloReply) {
                System.out.println("on next");
            }

            @Override
            public void onError(Throwable throwable) {
                System.out.println("on error");
            }

            @Override
            public void onCompleted() {
                System.out.println("on completed");
            }
        });
        System.out.println(r);
        // TODO 不理解怎么使用
    }

}
